<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form
        class="reg"
        :model="formData"
        :rules="formRules"
        ref="formRef"
        status-icon
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入账号"
            prefix-icon="el-icon-user"
            v-model="formData.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="formData.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            placeholder="请确认密码"
            prefix-icon="el-icon-lock"
            v-model="formData.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="submitForm"
            >注册</el-button
          >
          <el-link :underline="false" @click="$router.push('/login')"
            >去登录</el-link
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Reg',
  data () {
    //自定义校验规则---- 确认密码 :复制来的
    const samePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formData.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        username: '',
        password: '',
        repassword: ''
      },
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[0-9a-zA-Z]{1,10}$/,
            message: '请输入长度为1~10的字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码名', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '请输入长度为6~15的非空字符',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '请输入长度为6~15的非空字符',
            trigger: 'blur'
          },
          { validator: samePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //复制来的:通过ref获取整个表单组件,然后用表单的组件实例调用validate 完成兜底校验
    //
    submitForm () {
      this.$refs.formRef.validate(async valid => {
        //这里的valid 只有表单全部通过了 才会是true 进入if
        if (!valid) return
        if (valid) {
          //校验通过 发送ajax请求
          const { data: res } = await this.$axios.post(
            '/api/reg',
            this.formData
          )
          //   console.log(res)
          //发送成功 判断业务状态码
          //提示用户错误信息
          if (res.code !== 0) return this.$message.error(res.message)
          //提示用户登录成功
          this.$message.success(res.message)
          // 跳转到登录页
          this.$router.push('/login')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('@/assets/images/login_bg.jpg ') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .reg {
      padding: 0 25px;
    }
    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    }
  }
}
</style>
